<template>
  <div class="h-80vh mt-15" ref="chart"></div>
</template>

<script setup name="FoodCharts">
import { reqEchartsFood } from '@/api/echarts'
import * as echarts from 'echarts'
import { onMounted, ref, onUnmounted } from 'vue'
const chart = ref(null)
const echartsInstance = ref(null)
const initEchart = async () => {
  let { data } = await reqEchartsFood()
  const myChart = echarts.init(chart.value)
  echartsInstance.value = myChart

  const option = {
    title: {
      text: '美食数量',
    },
    tooltip: {
      trigger: 'item',
    },
    legend: {
      top: '5%',
      left: 'center',
    },
    series: [
      {
        encode: {
          itemName: 'title', // 'title' 对应你的对象属性名
          value: 'count', // 'count' 对应你的对象属性名
        },
        name: '美食数量',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
        },
        label: {
          show: false,
          formatter: '{b}: {c}',
          position: 'center',
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: 'bold',
          },
        },
        labelLine: {
          show: false,
        },
        data: data,
      },
    ],
  }

  myChart.setOption(option)
}
onMounted(() => {
  initEchart()
})
onUnmounted(() => {
  echartsInstance.value.dispose()
})
</script>

<style lang="scss" scoped></style>
